#include "ui_bofugui.h"
#include "bofugui.h"



/********************************************************************************************/

void BofuGui::on_pushButton_importFileList_clicked()
{
    //QString filelist="filelist.txt";
    QString sourceFile = QFileDialog::getOpenFileName(this, tr("Load FILELIST"), QDir::homePath());
    if (!sourceFile.isEmpty()){
        ui->lineEdit_list->setText( sourceFile+" loaded!!!" );
        ui->statusBar->showMessage(tr("filelist loaded"),5000);
        QString destinationFile=dir.absolutePath()+"/filelist.txt";
        copyFile(sourceFile,destinationFile);
        logDate();
        writeToLog("echo \"filelist loaded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        check_existing_files();

    }
}

void BofuGui::on_pushButton_loadWav_clicked()
{
    QString sourceFolder = QFileDialog::getExistingDirectory(this, tr("Choose wav-file folder"), QDir::homePath());
    if (!sourceFolder.isEmpty()){
        ui->lineEdit_wavFolder->setText( sourceFolder +" loaded!!!");
        QString command = "cp "+sourceFolder +"/*.wav "+dir.absolutePath()+"/wav/ >> "+dir.absolutePath()+"/"+korpusname+".log";
        commandexec = system(command.toAscii().data());
        ui->statusBar->showMessage(tr("wav-files loaded"),5000);


        writeToLog("echo \"wav-files loaded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        logDate();
        scriptDone();
    }
}

void BofuGui::on_pushButton_createFileList_clicked()
{
    QString filelist;
    if((settings.value("filelist").toString()=="")){
        filelist="filelist.txt";
    }else{
        filelist=(settings.value("filelist").toString());
    }
    command = "cd "+dir.absolutePath()+"; ls -1 wav/ | sed 's/\\(.*\\)\\..*/\\1/'  >> " +filelist.toUtf8().data();
    commandexec = system(command.toAscii().data());
    logDate();
    writeToLog("echo \"filelist created\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    ui->statusBar->showMessage(tr("filelist created"),5000);
    scriptDone();
    check_existing_files();
}

/********************************************************************************************/

void BofuGui::on_pushButton_importTranscription_clicked()
{
    QString sourceFile= QFileDialog::getOpenFileName(this, tr("Load transcription file"), QDir::homePath());
    if (!sourceFile.isEmpty()){

        ui->lineEdit_transcriptionImport->setText( sourceFile+" loaded!!!" );
        ui->statusBar->showMessage(tr("Transcriptionfile loaded"),5000);
        QString destinationFile=dir.absolutePath()+"/transcription.txt";
        copyFile(sourceFile,destinationFile);
        logDate();
        writeToLog("echo \"Transcriptionfile loaded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        check_existing_files();
    }
}


void BofuGui::on_pushButton_importOrthographic_clicked()
{
    QString sourceFile = QFileDialog::getOpenFileName(this, tr("Load orthografic file"), QDir::homePath());

    if (!sourceFile.isEmpty()){
        logDate();

        QString destinationFile=dir.absolutePath()+"/orthographic.txt";
        copyFile(sourceFile,destinationFile);
        ui->transcribeButton->setEnabled(1);
        ui->lineEdit_orthographicFile->setText( sourceFile +" loaded!!!");
        ui->statusBar->showMessage(tr("File loaded"),5000);
        writeToLog("echo \"orthographic file loaded\" >> "+dir.absolutePath()+"/"+korpusname+".log");
        check_existing_files();
    } else ui->transcribeButton->setEnabled(0);
}

void BofuGui::on_lexiconLoadButton_clicked()
{
    QString sourceFile= QFileDialog::getOpenFileName(this, tr("Loading lexicon file"), QDir::homePath());
    if (!sourceFile.isEmpty()){
        lexfile= sourceFile;
        ui->lexiconEdit->setText( sourceFile );
        ui->statusBar->showMessage(tr("Lex-File loaded"),5000);
        ui->correctBox->setEnabled(1);

    } else ui->correctBox->setEnabled(0);
}

void BofuGui::on_transcribeButton_clicked()
{
    logDate();
    ui->statusBar->showMessage(tr("File gets transcribed...please wait!"),5000);
    command = "cd "+dir.absolutePath()+"; perl "+bofu.absolutePath()+"/transcript/transcript.pl -sent orthographic.txt";
    if ((lexfile != "") && (ui->lexBox->isChecked())){
        command.append(" -lex "+lexfile);
    } else if (ui->lexBox->isChecked()){
        command.append(" -lex");
    }
    if (!ui->idBox->isChecked()){
        command.append(" -createIDs");
    }
    if (ui->correctBox->isChecked()){
        command.append(" -corr");
    }
    command.append(" >> "+dir.absolutePath()+"/"+korpusname+".log 2>&1");

    commandexec = system(command.toAscii().data());

    ui->statusBar->showMessage(tr("File was transcribed"),5000);
    writeToLog("echo \"File was transcribed\" >> "+dir.absolutePath()+"/"+korpusname+".log");
    scriptDone();
    check_existing_files();
}

